<template>
  <div class="item-one" ref="itemOne">
    <div class="title">
      <span>库存管理</span>
      <div>
        <el-button type="primary" @click="export_excel"><i class="el-icon-download"></i>导出</el-button>
        <el-button type="primary" v-if="eqma3 == true" @click="delete_all">批量删除</el-button>
        <el-button type="primary" v-if="eqma3 == true" @click="switch_all">批量切换</el-button>
        <el-button type="primary" v-if="eqma3 == true" @click="putStorage_all">批量入库</el-button>
        <el-button type="primary" @click="download_excel">模板下载</el-button>
        <el-upload
          class="upload-demo input-fil"
          :action="urls"
          :headers="token"
          :show-file-list="false"
          :on-success="uploadExcel">
        </el-upload>
        <el-button type="primary" class="excelBtn" @click="btnClick()">批量导入</el-button>
        <el-button type="primary" @click="eqma1 = true">新增入库</el-button>
        <el-button type="primary" @click="outbound()">出库</el-button>
      
      </div>
    </div>
    <div class="container">
      <div class="screeningBox">
        <div class="leftBox">
         
          <div class="leftitem">
            <el-select
            class="servicelevel"
            placeholder="设备状态"
            v-model="deviceState"
            >
                <el-option keys="0" label="绑定" value="0"></el-option>
                <el-option keys="1" label="出库" value="1"></el-option>
                <el-option keys="2" label="入库" value="2"></el-option>
            </el-select>
            <el-input class="merchants" @keyup.enter.native='soso' v-model.trim="deviceNo" placeholder="设备号"></el-input>
          </div>

<div class="leftitem">
   <el-select
            class="servicelevel"
            placeholder="入库类型"
            v-model="enterType"
            >
                <el-option keys="1" label="正常入库" value="1"></el-option>
                <el-option keys="2" label="其他入库" value="2"></el-option>
            </el-select>
            <el-select
            class="servicelevel"
            placeholder="出库类型"
            v-model="exitType"
            >
                <el-option keys="1" label="正常出库" value="1"></el-option>
                <el-option keys="2" label="其他出库" value="2"></el-option>
            </el-select>
</div>
         <div class="leftitem">
            <el-select class="servicelevel" placeholder="设备类型" v-model="deviceType" @change="chooseBrand">
              <el-option v-for="(item,index) in inventoryCategoryListlev" :key="index" :label="item.name" :value="item.id"></el-option>
            </el-select>
            <el-select class="servicelevel" placeholder="设备品牌" v-if="this.userinfo.rolesId==1||this.userinfo.rolesId==2||this.userinfo.rolesId==8" v-model="deviceBrand" >
              <el-option v-for="(item,index) in inventoryCategoryType" :key="index" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </div>   
        </div>
        <div class="rightBox">
          <div class="rightitem">
            <el-input class="merchants" @keyup.enter.native='soso' v-model.trim="batchNo" placeholder="请输入批次号"></el-input>
            <el-input class="merchants" @keyup.enter.native='soso' v-model.trim="merchantId" placeholder="请输入商户号或门店号（绑定编号）"></el-input>
          </div>
          <div class="rightitem">
           
            <el-form ref="conditions" :model="conditions">
              <el-select
                v-if="
                  userinfo.rolesId == 1 ||
                  userinfo.rolesId == 8 ||
                  userinfo.rolesId == 9
                "
                v-model="conditions.servicelevel"
                placeholder="服务商级别"
                @change="servicelevel"
              >
                <el-option keys="1" label="一级服务商" value="1"></el-option>
                <el-option keys="2" label="二级服务商" value="2"></el-option>
              </el-select>

              <el-autocomplete
                v-if="
                  userinfo.rolesId == 1 ||
                  userinfo.rolesId == 8 ||
                  userinfo.rolesId == 2 ||
                  userinfo.rolesId == 9
                "
                class="widthp30"
                v-model.trim="conditions.servicename"
                :fetch-suggestions="querySearch"
                placeholder="服务商名称"
                @select="serviceid"
              ></el-autocomplete>
              <el-autocomplete
               v-if="userinfo.rolesId != 5 && userinfo.rolesId != 4" 
                class="widthp30"
                v-model.trim="conditions.BDname"
                :fetch-suggestions="BDname"
                placeholder="业务经理"
                @select="BDid"
              ></el-autocomplete>
            </el-form>
          </div>
          <div class="rightitem">
            <el-button icon="el-icon-search" type="primary" @click="soso">搜索</el-button>
            <el-button icon="el-icon-refresh" type="primary" @click="reset">重置</el-button>
          </div>
        </div>
      </div>

      <div class="tableBox">
        <el-table
          ref="multipleTable"
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionChange"
          :data="tableData"
          :header-cell-style="tableHeaderColor"
        >
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column align="center" type="index" label="序号"></el-table-column>
          <el-table-column prop="batchNo" align="center" width="205" label="批次号"></el-table-column>
          <el-table-column prop="deviceNo" align="center" label="设备号"></el-table-column>
          <el-table-column prop="typeName" align="center" label="设备类型"></el-table-column>
          <el-table-column prop="deviceBrand" align="center" label="设备品牌"></el-table-column>
          <el-table-column prop="enterTime" align="center" width="160" label="上一次入库时间"></el-table-column>
          <el-table-column prop="exitTime" align="center" width="160" label="上一次出库时间"></el-table-column>
          <el-table-column prop="deviceHolder" align="center" label="分配对象"></el-table-column>
          <el-table-column prop="bindNo" align="center" label="绑定编号"></el-table-column>
          <el-table-column prop="status" align="center" label="设备状态">
            <template slot-scope="scope">
              <span v-if="scope.row.status == 0" style="color:#409EEF">绑定</span>
              <span v-if="scope.row.status == 1" style="color:#F7802C">出库</span>
              <span v-if="scope.row.status == 2" style="color:#259B24">入库</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="120">
            <template slot-scope="scope">
              <el-button type="text" v-if="scope.row.status == 1 || scope.row.status == 2" @click="editor(scope.row)">编辑</el-button>
              <el-button type="text" v-if="scope.row.status == 1" @click="putStorage_id(scope.row.id)">重新入库</el-button>
              <el-button type="text" v-if="scope.row.status == 2" @click="delete_id(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination">
          <el-pagination
            background
            @current-change="handleCurrentChange"
            @size-change="sizechange"
            :page-sizes="[5,10,20,30,40,50]"
            layout="total, sizes, prev, pager, next, jumper"
            :total="combined"
            :current-page.sync="cur_page"
          ></el-pagination>
        </div>
      </div>
      <br>
      <br>
      <!-- 出库-->
      <el-dialog v-dialogDrag title="出库" :visible.sync="eqma" width="35%" :close-on-click-modal="false">
        <el-form
          status-icon
          ref="facility"
          label-width="110px"
          class="demo-dynamic wid100"
        >
          <el-form-item label="已选择设备：" class="ck_label">
            <span v-for="(item,index) in multipleSelection" :key="index">{{item.deviceNo}}</span>
          </el-form-item>
          <el-form-item label="出库对象：">
            <el-select class="wid100" placeholder="请选择出库的角色类型" v-model="ck_deviceType" @change="rolename">
              <el-option keys="2" label="一级服务商" :value="2"></el-option>
              <el-option keys="3" label="二级服务商" :value="3"></el-option>
              <el-option keys="4" label="业务经理" :value="4"></el-option>
            </el-select>
          </el-form-item>
           <el-form-item label="对象名称：" >
              <!-- <el-input type="text"  autocomplete="off" v-model="ck_terminal"></el-input> -->
              <el-autocomplete
                class="wid100"
                v-model.trim="ck_terminal"
                :fetch-suggestions="querySearch1"
                @select="serviceid"
                placeholder="请输入服务商（业务经理）名称"
              ></el-autocomplete>
           </el-form-item>
           <el-form-item label="出库类型：" >
            <el-select class="wid100" placeholder="请选择出库类型" v-model="ck_exitType">
              <el-option keys="1" label="故障出库" value="1"></el-option>
              <el-option keys="2" label="终端划拨" value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="出库描述：" >
            <el-input type="text" placeholder="请填写出库描述" autocomplete="off" v-model.trim="ck_exitDesc"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="eqma = false">取消</el-button>
          <el-button type="primary" @click="devicesave()">确定</el-button>
        </span>
      </el-dialog>
      <!-- 入库 -->
      <el-dialog v-dialogDrag title="新增入库" :visible.sync="eqma1" width="35%" :close-on-click-modal="false">
        <el-form
          status-icon
          ref="facility"
          label-width="110px"
          class="demo-dynamic wid100"
        >
          <el-form-item label="设备类型：">
            <el-select class="wid100" placeholder="请选择设备类型" v-model="add_typeId" @change="chooseBrand1">
              <el-option v-for="(item,index) in inventoryCategoryListlev" :key="index" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="设备品牌：">
            <el-select class="wid100" placeholder="请选择设备品牌" v-model="add_brandId" @change="selsct_ruku">
              <el-option v-for="(item,index) in inventoryCategoryType" :key="index" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="设备号：" >
            <el-input type="text" placeholder="请填写设备号" autocomplete="off" v-model.trim="add_deviceNo"></el-input>
          </el-form-item>
          <el-form-item label="入库类型：" >
            <el-select class="wid100" placeholder="请选择入库类型" v-model="add_enterType">
              <el-option keys="1" label="正常入库" value="1"></el-option>
              <el-option keys="2" label="其他入库" value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="入库描述：" >
            <el-input type="text" placeholder="请填写入库描述" autocomplete="off" v-model.trim="add_enterDesc"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="eqma1 = false">取消</el-button>
          <el-button type="primary" @click="getInventoryImportSingle">确定</el-button>
        </span>
      </el-dialog>
      <!-- 编辑 -->
      <el-dialog v-dialogDrag title="编辑" :visible.sync="eqma2" width="35%" :close-on-click-modal="false">
        <el-form
          status-icon
          ref="facility"
          label-width="110px"
          class="demo-dynamic wid100"
        >
          <el-form-item label="设备类型：">
            <el-select class="wid100" placeholder="请选择设备类型" v-model="bj_deviceType" @change="chooseBrand2" :disabled="bj_eqma1">
              <el-option v-for="(item,index) in inventoryCategoryListlev" :key="index" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="设备品牌：">
            <el-select class="wid100" placeholder="请选择设备品牌" v-model="bj_deviceBrand" @change="selsct_bj" :disabled="bj_eqma1">
              <el-option v-for="(item,index) in inventoryCategoryType" :key="index" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="设备号：" >
            <el-input type="text" placeholder="请填写设备号" autocomplete="off" v-model.trim="bj_terminal" :disabled="bj_eqma1"></el-input>
          </el-form-item>
          <el-form-item label="出库对象：">
            <el-select class="wid100" placeholder="请选择出库的角色类型" v-model="bj_Type" @change="rolename1" :disabled="bj_eqma">
              <el-option keys="2" label="一级服务商" :value="2"></el-option>
              <el-option keys="3" label="二级服务商" :value="3"></el-option>
              <el-option keys="4" label="业务经理" :value="4"></el-option>
            </el-select>
          </el-form-item>
           <el-form-item label="对象名称：" :disabled="bj_eqma">
              <!-- <el-input type="text"  autocomplete="off" v-model="ck_terminal"></el-input> -->
              <el-autocomplete
                class="wid100"
                v-model.trim="bj_name"
                :fetch-suggestions="querySearch1"
                @select="serviceid"
                placeholder="请输入服务商（业务经理）名称"
                :disabled="bj_eqma"
              ></el-autocomplete>
           </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="eqma2 = false">取消</el-button>
          <el-button type="primary" @click="update">确定</el-button>
        </span>
      </el-dialog>
      <!-- 批量切换-->
      <el-dialog v-dialogDrag title="批量切换" :visible.sync="eqma4" width="35%" :close-on-click-modal="false">
        <el-form
          status-icon
          ref="facility"
          label-width="110px"
          class="demo-dynamic wid100"
        >
          <el-form-item label="出库对象：">
            <el-select class="wid100" placeholder="请选择出库的角色类型" v-model="qh_deviceType" @change="rolename2">
              <el-option keys="2" label="一级服务商" :value="2"></el-option>
              <el-option keys="3" label="二级服务商" :value="3"></el-option>
              <el-option keys="4" label="业务经理" :value="4"></el-option>
            </el-select>
          </el-form-item>
           <el-form-item label="对象名称：" >
              <!-- <el-input type="text"  autocomplete="off" v-model="ck_terminal"></el-input> -->
              <el-autocomplete
                class="wid100"
                v-model.trim="qh_terminal"
                :fetch-suggestions="querySearch1"
                @select="serviceid"
                placeholder="请输入服务商（业务经理）名称"
              ></el-autocomplete>
           </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="eqma4 = false">取消</el-button>
          <el-button type="primary" @click="switchAll()">确定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getservice, getBD, getInventory, getInventoryimport, getUserSelect, getInventoryCategoryListlev, getInventoryExport, getInventoryImportSingle, getInventoryUpdate, getInventoryBatchRemove, getInventoryBatchSwitchHolder, getInventoryBatchIn, getInventoryExcel} from "@/api/api";
export default {
  name: "item-one",
  data: function() {
    return {
      token: { token: sessionStorage.getItem("token") },
      tableData: [],
      fileList:[],
      buer:false,
      cur_page: 1,
      cur_size: 10,
      multipleSelection: [],
      inventoryCategoryListlev:[],//设备类型
      inventoryCategoryType:[],//设备品牌
      eqma: false,//出库按钮
      eqma1: false,//入库按钮
      eqma2: false,//编辑按钮
      eqma3: false,//多选显示按钮
      eqma4: false,//批量切换按钮
      visible: false,
      combined:0,
      deviceType:'',
      deviceBrand:'',
      deviceState:'',
      deviceNo:'',
      enterType: '',
      exitType: '',
      merchantId:'',//商户号
      shopId:'',//门店号
      batchNo:'',//批次号
      ck_merchants:'',
      ck_deviceType:'',
      ck_terminal:'',//所属终端
      ck_exitType: '',
      ck_exitDesc: '',
      servicearr: [],//服务商模糊查询
      BDarr: [],
      conditions: {
        servicelevel: "",
        servicename: "",
        BDname:"",
        BDid:''
      },
      serviceid1:'',//弹出框对象名称模糊查询 Fid
      restaurants1:[],//出库弹框的模糊查询
      roleId:'',
      roleId1:'',
      roleId2:'',
      add_typeId:'',//入库-设备类型
      add_typeName:'',//入库-设备类型
      add_brandId:'',//入库-设备品牌
      add_deviceNo:'',//入库-设备号
      add_enterType: '',
      add_enterDesc: '',
      bj_deviceType:'',
      bj_deviceBrand:'',
      bj_terminal:'',
      bj_Type:'',
      bj_name:'',
      bj_Id:'',
      bj_typeName:'',//编辑-设备类型文字
      serviceFid:'',//编辑弹出框 对象名称模糊查询 Fid
      bj_eqma: false,//编辑框控制出库对象框 显示隐藏按钮
      bj_eqma1: false,//编辑框控制出库对象框 显示隐藏按钮
      qh_deviceType:'',//批量切换 选择出库的角色类型
      qh_terminal:'',//批量切换 对象名称
      qh_ids:'',//批量切换 传递的id
      qh_serviceFid:'',//批量切换弹出框 对象名称模糊查询 Fid
      exxx: false,
      urls: process.env.VUE_APP_BASE_URL +"/product/v31/api/inventory/importBatch",
      
    };
  },
  created() {
   this.userinfo = JSON.parse(sessionStorage.getItem("userInfo"));
    this.$store.dispatch("getNewNum", 6);
    this.getInventory()
    if(this.userinfo.rolesId==1||this.userinfo.rolesId==2||this.userinfo.rolesId==8){
      this.getInventoryCategoryListlev()
    }
  },
  watch:{
    multipleSelection(){
      if(this.multipleSelection.length > 1){
        this.eqma3 = true
      }else{
        this.eqma3 = false
      }
    }
  },
  methods: {
    //修改thead 背景色
      tableHeaderColor({ row, column, rowIndex, columnIndex }) {
      if (rowIndex === 0) {
        return 'background-color: #F0F0F0;font-weight: 500;font-size:14px;color:#000;'
      }
    },
    // 刷新
    // refresh() {
    //   this.getInventory()
    // },
    // 分页导航
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getInventory()
    },
    // 分页数量
    sizechange(val) {
      this.cur_size = val;
      this.getInventory();
    },
    //多选框
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    //  重置
    reset() {
      this.conditions.servicelevel = ''
      this.conditions.servicename = ''
      this.conditions.BDname = ''
      this.deviceBrand = ''
      this.deviceNo = ''
      this.deviceType = ''
      this.deviceState = ''
      this.shopId = ''
      this.merchantId = ''
      this.serviceid1 = ''
      this.conditions.BDid = ''
      this.batchNo = '',
      this.enterType = '',
      this.exitType = ''
    },
    // 搜索
    soso() {
      this.cur_page = 1
      this.getInventory()
    },
    //获取设备类型和品牌
    getInventoryCategoryListlev(){
      this.inventoryCategoryListlev = []
      getInventoryCategoryListlev({
        lev:2
      }).then(res => {
        if(res.code == 405){
          sessionStorage. removeItem('token')
          this.$router.push('/login');
        }else if(res.code == 200){
          for(var i = 0; i < res.data.length; i++){
            this.inventoryCategoryListlev.push({id:res.data[i].id,name:res.data[i].name})
          }
        }else if(res.code == 500){
          this.$message.error(res.message);
        }
      })
    },
    getInventoryCategoryListlev1(v){
      this.inventoryCategoryType= []
      getInventoryCategoryListlev({
        pid:v,
        lev:3
      }).then(res => {
        if(res.code == 405){
          sessionStorage. removeItem('token')
          this.$router.push('/login');
        }else if(res.code == 200){
          for(var i = 0; i < res.data.length; i++){
            this.inventoryCategoryType.push({id:res.data[i].id,name:res.data[i].name})
          }
        }else if(res.code == 500){
          this.$message.error(res.message);
        }
      })
    },
    //根据设备类型选择对应品牌
    chooseBrand(v){ //外层搜索框事件
      this.deviceBrand = ''
      this.getInventoryCategoryListlev1(v)
    },
    chooseBrand1(v){ //新增入库弹框 获取设备品牌事件
      this.add_brandId = ''
      this.getInventoryCategoryListlev1(v)
    },
    chooseBrand2(v){ //单个编辑弹框 获取设备品牌事件
      this.bj_deviceBrand = ''
      this.bj_typeName = ''
      this.getInventoryCategoryListlev1(v)
    },
    //获取列表数据
    getInventory(){
      var agent1id = ''
      if(this.conditions.servicelevel !== '' && this.conditions.BDid == ''){
        agent1id = this.serviceid1
      }else if(this.conditions.BDid !== ''){
        agent1id = this.conditions.BDid
      }
      getInventory({
        page: this.cur_page,
        size: this.cur_size,
        brandId: this.deviceBrand,
        deviceNo: this.deviceNo,
        typeId: this.deviceType,
        status: this.deviceState,
        shopNo: this.shopId,
        batchNo: this.batchNo,
        // merchantNo: this.merchantId,
        bindNo: this.merchantId,
        // bdId: this.conditions.BDid,
        holderFid: agent1id,
        enterType: this.enterType,
        exitType: this.exitType,
        // agent2id: agent2id
      }).then(res => {
        if(res.code == 405){
          sessionStorage. removeItem('token')
          this.$router.push('/login');
        }else if(res.code == 200){
          this.tableData = res.data
          this.combined = res.pageInfo.recordCount
        }else if(res.code == 500){
          this.$message.error(res.message);
        }
      })
    },
    // 服务商级别
    servicelevel(val){
      this.servicearr = []
       getservice({
          type: val,
        }).then(res => {
          if(res.code == 405){
            sessionStorage. removeItem('token')
            this.$router.push('/login');
             loading.close();
             this.exxxx = false;
          }else if(res.code == 200){
            for(var i=0;i<res.data.rows.length;i++){
              this.servicearr.push({'value':res.data.rows[i].agentname,'id':res.data.rows[i].id})
            }
          }else{
            this.$message.error(res.message);
          }
        });
    },
    // 服务商名称
    querySearch(queryString, cb) {
      let servicearr = this.servicearr;
      let results = queryString
        ? servicearr.filter(this.createFilter(queryString))
        : servicearr;
      // 调用 callback 返回建议列表的数据
      cb(results);
    },
    // 业务经理
    BDname(queryString, cb){
      let BDarr = this.BDarr;
      let results = queryString
        ? BDarr.filter(this.createFilter(queryString))
        : BDarr;
      // 调用 callback 返回建议列表的数据
      cb(results);
    },
    BDid(item) {
      this.conditions.BDid = item.id
    },
    serviceid(item){
      this.serviceid1 = item.id
      this.serviceFid = item.id
      this.qh_serviceFid = item.id
      this.getBD(item.id)
    },
    getBD(id){
      this.BDarr = []
        getBD({
          agentid: id
        }).then(res => {
          if(res.code == 405){
            sessionStorage. removeItem('token')
            this.$router.push('/login');
             loading.close();
             this.exxxx = false;
          }else if(res.code == 200){
            for(var i=0;i<res.data.rows.length;i++){
              this.BDarr.push({'value':res.data.rows[i].name,'id':res.data.rows[i].id})
            }
          }else{
            this.$message.error(res.message);
          }
        });
    },
    createFilter(queryString) {
      return restaurant => {
        return (
          restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) !== -1
        );
      };
    },
    // 出库 获取设备号
    outbound(){
      this.ck_deviceType = ''
      this.roleId = ''
      this.roleId1 = ''
      this.roleId2 = ''
      this.ck_terminal = ''//弹出款显示时 把其他公用模糊查询的关键字清空
      this.bj_name = ''//弹出款显示时 把其他公用模糊查询的关键字清空
      this.qh_terminal = ''//弹出款显示时 把其他公用模糊查询的关键字清空
      if(this.multipleSelection.length > 0){
        var merchants = []
        for(var i = 0; i < this.multipleSelection.length; i++){
          if(this.multipleSelection[i].status == 0 || this.multipleSelection[i].status == 1){
            this.$message.error('已绑定和出库状态的设备不能再次出库');
            // this.eqma = false
            return false
          }else{
            // this.eqma = true
            merchants.push(this.multipleSelection[i].id)
          }
        }
        this.eqma = true
        this.ck_merchants = merchants.join(',')
      }else{
        this.$message.error('请选择设备');
      }
    },
    handleRemove(file, fileList) {
        console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    uploadExcel(res, file){
       const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      if(res.code ==405){
        loading.close();
        sessionStorage.removeItem("token");
        this.$router.push("/login");
      }else if (res.code == 200) {
        loading.close();
        this.getInventory()
        // this.$message({
        //   message: res.message,
        //   type: "success"
        // });
        this.$alert('<div>'+ res.message +'</div> <div> <a href='+ res.errorInfo +'>'+ res.errorInfo +'</a> </div>', '已入库', {
          confirmButtonText: '确定',
          dangerouslyUseHTMLString: true,
          callback: action => {
            this.$message({
              message: '已确定'
            });
          }
        });
      }else{
        loading.close();
        this.$message.error(res.message);
      }
    },
    btnClick() {
      document.querySelector(".el-upload__input").click();
      // this.buer = true
    },
    //单个入库
    getInventoryImportSingle(){
      getInventoryImportSingle({
        typeId: this.add_typeId,
        brandId: this.add_brandId,
        deviceNo: this.add_deviceNo,
        deviceBrand:this.add_typeName,
        enterType: this.add_enterType,
        enterDesc: this.add_enterDesc
      }).then(res => {
        if(res.code ==405){
          sessionStorage.removeItem("token");
          this.$router.push("/login");
        }else if (res.code == 200) {
          this.getInventory()
           this.add_typeId = ''
           this.add_brandId = ''
           this.add_deviceNo = ''
           this.add_typeName = ''
           this.add_enterType = ''
           this.add_enterDesc = ''
           this.eqma1 = false
           this.$message({
            message: "入库成功",
            type: "success"
          });
        }else{
            this.$message.error(res.message);
        }
      })
    },
    selsct_ruku(v){
      console.log(this.inventoryCategoryType)
      for(var i = 0 ; i < this.inventoryCategoryType.length ; i++){
        if(v == this.inventoryCategoryType[i].id){
          this.add_typeName = this.inventoryCategoryType[i].name
        }
      }
    },
    selsct_bj(v){ //编辑设备品牌下拉框 获取文字
      for(var i = 0 ; i < this.inventoryCategoryType.length ; i++){
        if(v == this.inventoryCategoryType[i].id){
          this.bj_typeName = this.inventoryCategoryType[i].name
        }
      }
    },
    //出库
    rolename(val){
      this.roleId = val
      this.roleId1 = ''
      this.roleId2 = ''
      this.ck_terminal = ''
      this.serviceid1 = '' //切换出库对象的时候把模糊搜索的关键字 FID都清空
      this.restaurants1 = []
    },
    rolename1(val){
      console.log(val)
      this.roleId1 = val
      this.roleId = ''
      this.roleId2 = ''
      this.bj_name = ''
      this.serviceFid = ''
      this.restaurants1 = []
    },
    rolename2(val){
      this.roleId2 = val
      this.roleId1 = ''
      this.roleId = ''
      this.qh_terminal = ''
      this.qh_serviceFid = ''
      this.restaurants1 = []
    },
    querySearch1(queryString, cb) {
        var restaurants = this.restaurants1;
        var results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants;
        // 调用 callback 返回建议列表的数据
        cb(results);
        var keywords = ''
        if(this.ck_terminal){
          keywords = this.ck_terminal
        }else if(this.bj_name){
          keywords = this.bj_name
        }else if(this.qh_terminal){
          keywords = this.qh_terminal
        }
        var roleIds = ''
        if(this.roleId){
          roleIds = this.roleId
          this.roleId1 = ''
          this.roleId2 = ''
        }else if(this.roleId1){
          roleIds = this.roleId1
          this.roleId = ''
          this.roleId2 = ''
        }else if(this.roleId2){
          roleIds = this.roleId2
          this.roleId1 = ''
          this.roleId = ''
        }
        getUserSelect({
          roleId: roleIds,
          keywords: keywords,
        }).then(res => {
          console.log(res.data)
          if(res.code == 200){
            this.restaurants1 = []
            for(var i=0;i<res.data.length;i++){
              this.restaurants1.push({'value':res.data[i].fname,'id':res.data[i].fid})
            }
            let results = queryString
              ? this.restaurants1.filter(this.createFilter(queryString))
              : this.restaurants1;
            // 调用 callback 返回建议列表的数据
            cb(results);
          }
        })
    },
    devicesave(){
      getInventoryExport({
        ids: this.ck_merchants,
        holderRole: this.ck_deviceType,
        deviceHolder: this.ck_terminal,
        holderFid: this.serviceid1
      }).then(res => {
        if(res.code ==405){
          sessionStorage.removeItem("token");
          this.$router.push("/login");
        }else if (res.code == 200) {
          this.ck_merchants = ''
          this.ck_deviceType = ''
          this.ck_terminal = ''
          this.serviceid1 = ''
          this.eqma = false
          this.getInventory()
          this.$message({
            message: "出库成功",
            type: "success"
          });
        }else{
            this.$message.error(res.message);
        }
      })
    },
    //编辑
    editor(v){
      this.eqma2 = true
      this.roleId = ''
      this.roleId1 = ''
      this.roleId2 = ''
      this.ck_terminal = ''//弹出款显示时 把其他公用模糊查询的关键字清空
      this.bj_name = ''//弹出款显示时 把其他公用模糊查询的关键字清空
      this.qh_terminal = ''//弹出款显示时 把其他公用模糊查询的关键字清空
      this.bj_deviceType = v.typeId
      this.bj_deviceBrand = v.brandId
      this.bj_terminal = v.deviceNo
      if(v.holderRole == -1){
        this.bj_Type = ''
      }else{
        this.bj_Type = v.holderRole
      }
      this.bj_name = v.deviceHolder
      this.bj_Id = v.id
      this.serviceFid = v.holderFid
      this.bj_typeName = v.deviceBrand
      this.getInventoryCategoryListlev1(v.typeId)
      // console.log(this.BDarr)
      // console.log(this.inventoryCategoryType)
      this.roleId = v.holderRole //点击编辑时 把模糊搜索的 roleId 换成当前的出库对象
      this.restaurants1 = []
      if(v.status == 2){//入库状态编辑不能修改出库对象
        this.bj_eqma = true
      }else{
        this.bj_eqma = false
      }
      if(v.status == 1){//入库状态编辑不能修改出库对象
        this.bj_eqma1 = true
      }else{
        this.bj_eqma1 = false
      }
    },
    //编辑提交
    update(){
      getInventoryUpdate({
        typeId: this.bj_deviceType,//设备类型ID
        brandId: this.bj_deviceBrand,//设备品牌ID
        deviceBrand: this.bj_typeName,
        deviceNo: this.bj_terminal, //设备编号
        holderRole: this.bj_Type,//出库对象角色ID
        holderFid: this.serviceFid,//出库对象Fid
        deviceHolder: this.bj_name,
      },this.bj_Id).then(res => {
        if(res.code ==405){
          sessionStorage.removeItem("token");
          this.$router.push("/login");
        }else if (res.code == 200) {
          this.eqma2 = false
          this.restaurants1 = []
          this.getInventory()
          this.$message({
            message: "修改成功",
            type: "success"
          });
        }else{
            this.$message.error(res.message);
        }
      })
    },
    //删除
    delete(v){
      this.$confirm(`是否确定删除设备？`, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
      }).then(() => {
          getInventoryBatchRemove({
            ids:v
          }).then(res =>{
            if(res.code == 405){
              sessionStorage. removeItem('token')
              this.$router.push('/login');
            }else if(res.code == 200){
              this.$message({
                message: res.message,
                type: 'success'
              });
              this.getInventory()
            }else if(res.code == 500){
              this.$message.error(res.message);
            }
          })
      })
      .catch(() => {
          this.$message({
              type: "info",
              message: "已取消"
          });
      });
    },
    //单个删除
    delete_id(v){
      this.delete(v)
    },
    //批量删除
    delete_all(){
      if(this.multipleSelection.length > 0){
        var merchants = []
        for(var i = 0; i < this.multipleSelection.length; i++){
          if(this.multipleSelection[i].status == 0 || this.multipleSelection[i].status == 1){
            this.$message.error('已绑定和出库状态的设备不能删除');
            return false
          }else if(this.multipleSelection[i].status == 2){
            merchants.push(this.multipleSelection[i].id)
          }
        }
        this.delete(merchants.join(','))
      }else{
        this.$message.error('请选择设备');
      }
    },
    //批量切换
    switch_all(){
      this.qh_deviceType = ''
      this.qh_terminal = ''
      this.ck_terminal = ''//弹出款显示时 把其他公用模糊查询的关键字清空
      this.bj_name = ''//弹出款显示时 把其他公用模糊查询的关键字清空
      this.roleId = ''
      this.roleId1 = ''
      this.roleId2 = ''
      console.log(this.multipleSelection)
      if(this.multipleSelection.length > 0){
        var merchants = []
        for(var i = 0; i < this.multipleSelection.length; i++){
          if(this.multipleSelection[i].status == 0 || this.multipleSelection[i].status == 2){
            this.$message.error('入库和已绑定状态的设备不能修改');
            return false
          }else if(this.multipleSelection[i].status == 1){
            merchants.push(this.multipleSelection[i].id)
          }
        }
        this.eqma4 = true
        this.qh_ids = merchants.join(',')
      }else{
        this.$message.error('请选择设备');
      }
    },
    switchAll(){
      getInventoryBatchSwitchHolder({
        ids: this.qh_ids,
        holderRole: this.qh_deviceType,
        deviceHolder: this.qh_terminal,
        holderFid: this.qh_serviceFid
      }).then(res => {
        if(res.code == 405){
          sessionStorage. removeItem('token')
          this.$router.push('/login');
        }else if(res.code == 200){
          this.eqma4 = false
          this.restaurants1 = []
          this.qh_deviceType = ''
          this.qh_terminal = ''
          this.$message({
            message: res.message,
            type: 'success'
          });
          this.getInventory()
        }else if(res.code == 500){
          this.$message.error(res.message);
        }
      })
    },
    //批量重新入库
    putStorage(v){
      this.$confirm(`是否确定修改设备状态？`, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
      }).then(() => {
          getInventoryBatchIn({
            ids:v
          }).then(res =>{
            if(res.code == 405){
              sessionStorage. removeItem('token')
              this.$router.push('/login');
            }else if(res.code == 200){
              this.$message({
                message: res.message,
                type: 'success'
              });
              this.getInventory()
            }else if(res.code == 500){
              this.$message.error(res.message);
            }
          })
      })
      .catch(() => {
          this.$message({
              type: "info",
              message: "已取消"
          });
      });
    },
    //单个重新入库
    putStorage_id(v){
      this.putStorage(v)
    },
    //批量重新入库
    putStorage_all(){
      if(this.multipleSelection.length > 0){
        var merchants = []
        for(var i = 0; i < this.multipleSelection.length; i++){
          if(this.multipleSelection[i].status == 0 || this.multipleSelection[i].status == 2){
            this.$message.error('已绑定和入库状态的设备不能重新入库');
            return false
          }else if(this.multipleSelection[i].status == 1){
            merchants.push(this.multipleSelection[i].id)
          }
        }
        this.putStorage(merchants.join(','))
      }else{
        this.$message.error('请选择设备');
      }
    },
    //下载模板
    download_excel(){
      let elink = document.createElement('a');
      elink.download = "库存模板.xlsm";
      elink.href = 'https://sb-merchant.oss-cn-hangzhou.aliyuncs.com/库存模板.xlsm';
      elink.click();
    },
    //导出
    export_excel(){
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      var agent1id = ''
      if(this.conditions.servicelevel !== '' && this.conditions.BDid == ''){
        agent1id = this.serviceid1
      }else if(this.conditions.BDid !== ''){
        agent1id = this.conditions.BDid
      }
      this.exxx = true;
        getInventoryExcel({
          brandId: this.deviceBrand,
          deviceNo: this.deviceNo,
          typeId: this.deviceType,
          status: this.deviceState,
          shopNo: this.shopId,
          batchNo: this.batchNo,
          bindNo: this.merchantId,
          holderFid: agent1id,
        }).then(res => {
        if(res.data.code == 405){
          sessionStorage. removeItem('token')
          this.$router.push('/login');
          loading.close();
          this.exxx = false;
        }else if(res.data.code == 200){
          let elink = document.createElement('a');
          elink.href = res.data.data;
          elink.click();
          loading.close();
          this.exxx = false;
        }else{
          this.$message.error(res.data.message);
          loading.close();
          this.exxx = false;
        }
      })
    },
    // exportData(obj) {
    //   let _this = this;
    //   let inputDOM = this.$refs.inputer;
    //   this.file = event.currentTarget.files[0];
    //   var rABS = false; //是否将文件读取为二进制字符串
    //   var f = this.file;
    //   var reader = new FileReader();
    //   FileReader.prototype.readAsBinaryString = function(f) {
    //     var binary = "";
    //     var rABS = false; //是否将文件读取为二进制字符串
    //     var pt = this;
    //     var wb; //读取完成的数据
    //     var outdata;
    //     var reader = new FileReader();
    //     reader.onload = function(e) {
    //         var bytes = new Uint8Array(reader.result);
    //         var length = bytes.byteLength;
    //         for(var i = 0; i < length; i++) {
    //             binary += String.fromCharCode(bytes[i]);
    //         }
    //         var XLSX = require('xlsx');
    //         if(rABS) {
    //             wb = XLSX.read(btoa(fixdata(binary)), { //手动转化
    //                 type: 'base64'
    //             });
    //         } else {
    //             wb = XLSX.read(binary, {
    //                 type: 'binary'
    //             });
    //         }
    //         outdata = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]);//outdata就是你想要的东西
    //         this.da = [...outdata]
    //         let arr = []
    //         this.da.map(v => {
    //           // console.log(v)
    //             let obj = {}
    //             obj.id = v.设备号
    //             obj.status = v.设备类型
    //             obj.type = v.设备品牌
    //             arr.push(obj)
    //         })
    //         console.log(arr)
    //         // if(arr.id == undefined || arr.status== undefined || arr.type== undefined){
    //         //   _this.$message({
    //         //       message: '请导入正确数据',
    //         //       type: 'success'
    //         //   });
    //         //   return false
    //         // }
    //         let para = {
    //             //withList: JSON.stringify(this.da)
    //             withList: arr
    //         }
    //         _this.$message({
    //             message: '请耐心等待导入成功',
    //             type: 'success'
    //         });
    //         // withImport(para).then(res => {
    //         //     window.location.reload()
    //         // })
    //         arr = []
    //     }
    //     reader.readAsArrayBuffer(f);
    //     } 
    //     if(rABS) {
    //     reader.readAsArrayBuffer(f);
    //     } else {
    //         reader.readAsBinaryString(f);
    //     }
    // },
      ccc (content, filename) {
          var eleLink = document.createElement('a');
          eleLink.download = filename;
          eleLink.style.display = 'none';
          // 字符内容转变成blob地址
          var blob = new Blob([content]);
          eleLink.href = URL.createObjectURL(blob);
          // 触发点击
          document.body.appendChild(eleLink);
          eleLink.click();
          // 然后移除
          document.body.removeChild(eleLink);

    }
  }
};
</script>
<style scoped lang='less'>
.title {
  background: #fff;
  padding: 10px 10px;
  font-size: 18px;
  font-weight: 500;
  display: flex;
  justify-content: space-between;
}
.name {
  width: 25%;
}
.screeningBox {
  width: 100%;
  display: flex;
}
.leftBox,
.rightBox {
  width: 50%;
  padding: 0 15px;
  box-sizing: border-box;
}
.leftBox > .leftitem {
  margin-bottom: 15px;
}
.rightBox > .rightitem {
  margin-bottom: 15px;
}
.merchants {
  width: 40%;
}
.tableBox {
  margin-top: 20px;
}
.servicename {
  width: 20%;
}
.servicelevel {
  width: 40%;
}
.width20 {
  width: 20%;
}
.red {
  color: #f9607d;
}
.blue {
  color: #01aaef;
}
.stitle {
  padding: 5px 0;
  font-size: 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  i {
    margin-left: 10px;
    margin-right: 5px;
  }
  .swarning {
    color: red;
  }
  .warning {
    color: #f9607d;
  }
}
.wid100{
  width: 100% !important;
}
.upload-demo {
  display: none !important;
}
.excelBtn {
  margin-left: 10px;
}
// .ck_label{
.el-form-item__content{
  width: 100%;
  // height: 50px;
  display: -webkit-flex;
  display: flex;
  -webkit-flex-direction: row;
  flex-direction: row;
  flex-wrap: wrap;
  span{
    width: 30%;
    height: 100%;
    display:inline-block;
    text-align: center;
    line-height: 20px;
    background-color: rgb(231, 231, 231);
    margin: 5px;
  }
}
.BDname{
  width: 40%;
}
</style>
